#pragma once
#include <iostream>
using namespace std;
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>

// 如何获取时间——时间戳

#define SIZE 1024

#define Info 0
#define Debug 1
#define Waring 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3

#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        printStyle = Screen;
        path = "./log/";
    }
    ~Log()
    {
    }

    void printOneFile(const string &logname, const string &logtxt)
    {
        string _logname = path + logname;
        int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void printClassFile(int level, const string &logtxt)
    {
        string filename = LogFile;
        filename += ".";
        filename += levelToString(level); // "log.txt.Debug/Waring/Fatal"

        printOneFile(filename, logtxt);
    }

    void printLog(int level, const string logtxt)
    {
        switch (printStyle)
        {
        case Screen:
            cout << logtxt;
            break;
        case Onefile:
            printOneFile(LogFile, logtxt);
            break;
        case Classfile:
            printClassFile(level, logtxt);
            break;
        default:
            break;
        }
    }
    void Enable(int method)
    {
        printStyle = method;
    }
    string levelToString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
        case Debug:
            return "Debug";
        case Waring:
            return "Warning";
        case Fatal:
            return "Fatal";
        case Error:
            return "Error";
        default:
            return "None";
        }
    }

    // // 未来打印日志直接格式化输出
    // void message(int level, const char *format, ...) // 日志的等级  //打印格式 //可变参数
    // {
    //     // 获取时间
    //     // time函数， 单纯打印时间戳
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     char leftbuffer[SIZE];

    //     snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), ctime->tm_year, ctime->tm_mon, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

    //     va_list s;
    //     va_start(s, format);

    //     char rightbuffer[SIZE * 2];
    //     vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    //     va_end(s);

    //     char logtxt[SIZE * 2];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
    //     // printf("%s", logtxt);
    //     printLog(level, logtxt);
    // }

    void operator()(int level, const char *format, ...)
    {
        // 获取时间
        // time函数， 单纯打印时间戳
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];

        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), ctime->tm_year, ctime->tm_mon, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        va_list s;
        va_start(s, format);

        char rightbuffer[SIZE * 2];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
        // printf("%s", logtxt);
        printLog(level, logtxt);
    }

private:
    int printStyle; // 打印风格
    string path;
};
